<?php

require_once("category.php");
require_once("form.php");
require_once("DatabaseUtils.php");

/**
 * This class is the model for the post table in the database.
 * It is used to access the database table directly.  The post 
 * table should not be accessed anywhere else in the project.
 * 
 * @author Matt Makins
 * 
 * @param String $title
 * @param String $author
 * @param String $entry
 * @param Date $post_date
 * @param int $category_id
 */
 class Post extends BaseModel{

	private $id;
	private $title;
	private $author;
	private $entry;
	private $post_date;
	private $category_id;
	private $allow_comments;
	
	public function Post($id, $title, $author, $entry, $post_date, $category_id, $allow_comments){
		
		$this->id = $id;
		$this->title = $title;
		$this->author = $author;
		$this->entry = $entry;
		$this->post_date = $post_date;
		$this->category_id = $category_id;		
		$this->allow_comments = $allow_comments;
	}

	/**
	 * returns all of the blog posts as Post objects, 
	 * ordered by the post date
	 * 
	 * @return array of Post objects
	 */
	public static function findAllPosts(){
		
		Post::connect();
		$posts = array();
		$find = mysql_query("SELECT * FROM post ORDER BY date DESC");
		while ($row = mysql_fetch_array($find)){
 			$i = $row['id'];
 			$t = $row['title'];
 			$a = $row['author'];
 			$e = $row['message'];
 			$pd = $row['date'];
 			$ci = $row['category_id'];
 			$cm = $row['comments'];
 			array_push($posts, new Post($i,$t, $a, $e, $pd, $ci, $cm));
 		}
		return $posts;
	}
	
	/**
	 * adds a new post to the database
	 * 
	 * @param $title
	 * @param $post
	 * @param $cat
	 */
	public static function addPost($title, $post, $cat, $comm){
		
		// there must be a better way of doing this!!
		// I want to pass the form name and value to 
		// validate_presence_of
		$t = new FormItem($title, "Title");
		$p = new FormItem($post, "Post Content");
		$c = new FormItem($cat, "Category");
		
		if(Post::validate_presence_of($t,$p,$c)){

			$date = date("Y-m-d H:m:s");
			$author = "matt";  // TODO: make the author the current session username

			Post::connect();
			mysql_query("INSERT INTO post (title, message, date, author, category_id, comments) VALUES" .
					" ('$title', '$post', '$date', '$author', '$cat', $comm)");
			mysql_close();
			return true;
		}
		return false;
	}	

	/**
	 * allows an existsing post to be edited.
	 * 
	 * @param $title
	 * @param $post
	 * @param $category
	 * @param $id
	 * @param $comments
	 * 
	 * @return boolean
	 */
	public static function editPost($title, $post, $cat, $id, $comm){
		
		if(parent::validate_presence_of($title,$post,$cat,$id)){

			Post::connect();
			mysql_query("UPDATE post SET title = '$title', " .
					                    "message = '$post', " .
					                    "category_id = '$cat'" .
					                    "comment = '$comm$'" .
					    "WHERE id = '$id'");
			mysql_close();
			return true;
		}
		return false;
	}	

	
	/**
	 * returns the category name for the associated category id
	 * 
	 * @return String category name
	 */
	public function getCategoryName(){
		
		$catID = $this->getCategoryId();		
		$find =  mysql_query("SELECT * FROM category WHERE id = '$catID'");
		while ($row = mysql_fetch_array($find)){
			return rtrim(ltrim($row['name']));
		}
		// TODO: maybe throw a CategoryNotFoundException here?
		return "Category not found!";
	}
	
	/**
	 * returns the number of posts present in the database
	 * 
	 * @return integer number of posts in db
	 */	
	public static function getNumberOfPosts(){
		
		if (DatabaseUtils::databaseExists() && DatabaseUtils::tableExists(DB_POST_TABLE)){
			Post::connect();
			return mysql_num_rows(mysql_query("SELECT * FROM post"));
		}
		return 0;
	}

	public function getId(){
		return $this->id;	
	}
	
	public function getAuthor(){
		return $this->author;
	}
	
	public function getTitle(){
		return $this->title;
	}
	
	public function getEntry(){
		return $this->entry;
	}
	
	public function getPostDate(){
		return $this->post_date;
	}
	
	public function getCategoryId(){
		return $this->category_id;
	}
	
	public function commentsAllowed(){
		return $this->allow_comments;
	}
}
?>
